package com.glorysoft.tonglingxjpda.ui.function.viewmodel

import androidx.databinding.ObservableArrayList
import com.glorysoft.hlgdpda.entity.ui.CallObserveBean
import com.glorysoft.tonglingxjpda.BR
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.base.BaseViewModel
import com.glorysoft.tonglingxjpda.entity.BatchListEntity
import com.glorysoft.tonglingxjpda.entity.OrderListEntity
import com.glorysoft.tonglingxjpda.entity.ui.ToastMessageBean
import com.glorysoft.tonglingxjpda.model.TaskRepository
import com.google.gson.JsonObject
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: TrackViewmodel
 * Author: XJH
 * Date: 2021/9/15 11:15
 * Description:
 * History:
 */
class TrackViewModel : BaseViewModel() {

    private val trackRepository = TaskRepository()
    val batchTitle = arrayListOf("BatchID", "批次", "刻印号", "部件名称", "部件描述","数量","状态","Hold状态","工步描述","流程名称","数据采集","出荷拍照")

    val callback = object : OnEdcClick{
        override fun setOnEdcClick(item: BatchListEntity.BatchInfoEntity) {
            defUI.callObserve.value = CallObserveBean(0,item)
        }

        override fun setOnPicClick(item: BatchListEntity.BatchInfoEntity) {
            defUI.callObserve.value = CallObserveBean(1,item)
        }

    }

    //order 批次列表
    var batchItems = ObservableArrayList<BatchListEntity.BatchInfoEntity>()
    var batchBinding =
        ItemBinding.of<BatchListEntity.BatchInfoEntity>(BR.batchItem, R.layout.item_batch_info).bindExtra(BR.edcClick,callback)


    fun submit(batchID: String, type: String) {
        if (type == "出站"){
            batchItems.forEach {
                if (it.EDCFLAG == "Y"){
                    if (it.ISEDC == "N"){
                        defUI.toastEvent.value = ToastMessageBean("有数据采集未完成，请先进行数据采集",false)
                        return
                    }
                }
            }
        }
        val map = JsonObject()
        map.addProperty("BATCHID", batchID)
        map.addProperty("ACTIONTYPE", if (type == "进站")
            "DEFAULTTRACKIN" else "DEFAULTTRACKOUT")
        launchOnlyResult({
            trackRepository.track(
                map, if (type == "进站")
                    "TL.TLTRACKIN" else "TL.TLLOTTRACKOUT"
            )

            defUI.toastEvent.value = ToastMessageBean(if (type == "进站")
                "进站成功" else "出站成功",true)
            defUI.startActivity.call()
        })
    }

    fun getInfo(batchID: String){
        val map = JsonObject()
        map.addProperty("ACTIONTYPE","LOTINFOBYBATCHID")
        map.addProperty("BATCHID",batchID)
        launchOnlyResult({
            val batchInfo = trackRepository.getBatchInfo(map)
            batchItems.clear()
            batchItems.addAll(batchInfo.LOTLIST)
        })
    }

    interface OnEdcClick{
        fun setOnEdcClick(item : BatchListEntity.BatchInfoEntity)
        fun setOnPicClick(item : BatchListEntity.BatchInfoEntity)
    }

}